package com.egdigital.allzic.service;

import android.media.AudioTrack;

import com.spoledge.aacdecoder.MultiPlayer;
import com.spoledge.aacdecoder.PlayerCallback;

public class PlayerRadio implements PlayerCallback {

	private static final String	TAG	= PlayerRadio.class.getSimpleName();	// log
	private MultiPlayer			mPlayer;
	private StreamingState		mState;
	private RadioPlayerCallBack	mCallback;
	private AudioTrack			mAudioTrack;

	static interface RadioPlayerCallBack {

		public void radioPlayerStarted();

		public void radioPlayerStopped(int perf);

		public void radioPlayerException(Throwable e);

		public void radioPlayerMetadata(String key, String value);

		public void radioPlayerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs, int audioBufferCapacityMs);
	}

	public PlayerRadio() {
		super();
	}

	public void setCallbacks(RadioPlayerCallBack callback) {
		mCallback = callback;
	}

	public void start(String url) throws Exception {
		if (mPlayer == null) {
			mPlayer = new MultiPlayer(this);
		}
		try {
			mPlayer.playAsync(url);
			setState(StreamingState.PREPARING);
		} catch (Exception e) {
			setState(StreamingState.ERROR);
			throw e;
		}
	}

	public void stop() throws Exception {
		if (mPlayer == null) {
			setState(StreamingState.ERROR);
			return;
		}
		try {
			mPlayer.stop();
			setState(StreamingState.STOPPING);
		} catch (Exception e) {
			setState(StreamingState.ERROR);
			throw e;
		}
	}

	public StreamingState getState() {
		return mState;
	}

	public boolean isCreated() {
		return (mState == StreamingState.CREATED);
	}

	public boolean isEmpty() {
		return (mState == StreamingState.EMPTY);
	}

	public boolean isStopped() {
		return (mState == StreamingState.STOPPED);
	}

	public boolean isStarted() {
		return (mState == StreamingState.STARTED);
	}

	public boolean isPaused() {
		return (mState == StreamingState.PAUSED);
	}

	public boolean isPrepared() {
		return (mState == StreamingState.PREPARED);
	}

	public boolean isPreparing() {
		return (mState == StreamingState.PREPARING);
	}

	public boolean isStopping() {
		return mState == StreamingState.STOPPING;
	}

	public void setState(StreamingState state) {
		mState = state;
	}

	/**
	 * callbacks MultiPlayer
	 */
	@Override
	public void playerAudioTrackCreated(AudioTrack audioTrack) {
		mAudioTrack = audioTrack;
	}

	@Override
	public void playerException(Throwable e) {
		setState(StreamingState.ERROR);
		mCallback.radioPlayerException(e);
	}

	@Override
	public void playerMetadata(String key, String value) {
		mCallback.radioPlayerMetadata(key, value);
	}

	@Override
	public void playerPCMFeedBuffer(boolean isPlaying, int audioBufferSizeMs, int audioBufferCapacityMs) {
		// mCallback.radioPlayerPCMFeedBuffer(isPlaying, audioBufferSizeMs, audioBufferCapacityMs);
	}

	@Override
	public void playerStarted() {
		if (!isStopping()) {
			mCallback.radioPlayerStarted();
		}
		mState = StreamingState.STARTED;
	}

	@Override
	public void playerStopped(int perf) {
		if (mAudioTrack != null) {
			mAudioTrack.release();
			mAudioTrack.flush();
			mAudioTrack = null;
		}
		mCallback.radioPlayerStopped(perf);
		mState = StreamingState.STOPPED;
	}
}
